package jl.com.aliyun.check;

import jl.com.aliyun.config.ConfigProperties;
import jl.com.aliyun.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class CodeCheck {
    private final static Logger log = LoggerFactory.getLogger(CodeCheck.class);
    /**
     * 验证码破解
     * @param srcBase64
     */
    public static String checkCode(String srcBase64) {
//        String host = "http://api.3023data.com";
//        String path = "/ocr/captcha";
//        String method = "POST";
//        String key = "7155363afad54b4bd2c787096ab686ad";

        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("key", ConfigProperties.resource.getString("key"));
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
//        bodys.put("image", Base64Convert.imgBase64(url));//Base64Convert.imgBase64(url)
        bodys.put("image",srcBase64);
        bodys.put("length", "6");//0 | "" 为自动
        bodys.put("type", "1001");
        String body = "";
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(ConfigProperties.resource.getString("host"), ConfigProperties.resource.getString("path"), ConfigProperties.resource.getString("method"), headers, querys, bodys);
            log.info(response.toString());
            //获取response的body
//          System.out.println(EntityUtils.toString(response.getEntity()));
            body = EntityUtils.toString(response.getEntity());;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return body;
    }
}
